
public class MailFilter
{
public static writeEmailToFile(File file,String data)
{
	FileWriter fileWriter = null;
	BufferedWriter bufferedWriter = null;
	try
	{
		fileWriter = new FileWriter(file);
		bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(data);
		bufferedWriter.newLine();
		bufferedWriter.close();
		
		return fileWriter;
	
	/*else
	{
		file.createFile();
		fileWriter = new FileWriter(file);
		bufferedWriter.write(data);
		bufferedWriter.newLine();
		bufferedWriter.close();
	}*/
	}catch(IOException ex)
	{
		System.out.println("error writing to file '" + file.getName() + "'" + ex.toString());
		return false;
	}
	
	finally
	{
		try
		{
			bufferedWriter.flush();
			bufferedWriter.close();
			fileWriter.flush();
			fileWriter.close();
		}catch(Exception ex)
		{
			bufferdWriter = null;
			fileWriter = null;
			e.printStackTrace();
		}
	
	}
}
		
public static String[] filterEmails(String[] emailList,String filterString)
{
	String[] result = new String[emailList.length];
	for(int i=0;i<emailList.length;i++)
	{
		if(emailList[i] != null)
		{
			if(emailList[i].contains(filterString))
			{
				result[i] = emailList[i];
			}
		}
	return result;
}
}
/*
public static String[] retriveEmails(File file)
{
		String line = null;
		String[] EmailList = new String[20];
		int i=0;
		try
		{
			FileReader fileReader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			while((line = bufferedReader.readLine()) != null)
			{
				EmailList[i] = list.toString();
			}
			bufferedReader.close();
			return EmailLists;
		}
		catch(FileNotFoundException ex)
		{
			System.out.println("unable to open file '" + file.getName() + "'");
		}
		catch(IOException ex)
		{
			System.out.println("error in reading file '" + file.getName() + "'");
		}
		return null;
}*/
	
	public static void main(String[] felight)
	{
		String[] result = retriveEmails(newFile("test.txt"));
		for(int i=0;i<result.length;i++)
		{
			if(result[i] != null)
			{
				System.out.println("results[i]");
			}
		WriteEmailToFile(new File("outputMail.txt"),result[i]);
		}
		System.out.println(result[i]);
		System.out.println("emails after filter");
		String[] result = filterEmails(emails,"yahoo");
	}
	
	
	}
	